package cn.doctorpda.conference.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import cn.doctorpda.conference.MyApplication;
import cn.doctorpda.conference.localstore.ClientInfo;
import cn.doctorpda.conference.localstore.UserInfo;
import cn.doctorpda.conference.ui.SwitchAccountActivity;
import cn.doctorpda.conference.ui.common.DetailActivity;
import cn.doctorpda.conference.ui.splash.SplashActivity;
import cn.doctorpda.conference.utils.AESUtil;
import cn.doctorpda.conference.utils.L;

/**
 * User DELL
 * Date 2016/5/18.
 */
public class StartReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        String username = extras.getString("username");
        String password = extras.getString("password");
        password = AESUtil.decrypt(password, "doctorpda8888888");

        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
            if (UserInfo.getUserInfo() != null) {
                //用户已经登录，并且与参数中是同一个用户 那么进入相应的详情页
                L.d(username + password);
                if (TextUtils.equals(username, UserInfo.getUserInfo().getMOBILE())) {
                    toThird(extras, context);
                } else {
                    Intent i = new Intent(context, SwitchAccountActivity.class);
                    i.putExtras(intent.getExtras());
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
            } else {
                Intent i = new Intent(context, SwitchAccountActivity.class);
                i.putExtras(intent.getExtras());
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }
        } else {
            Intent iss = new Intent(context, SplashActivity.class);
            iss.putExtras(intent.getExtras());
            iss.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(iss);
        }
    }

    void toThird(Bundle bundle, Context context) {
        int flag = bundle.getInt("flag", -1);
        L.d("拉起flag" + flag);
        if (flag == 2) {
            toMain(context);
        } else if (flag == 4) {
            //进入各种详情页面
            String id = bundle.getString("id");
            String type = bundle.getString("type");
            if (TextUtils.isEmpty(type)) {
                toMain(context);
                return;
            }
            if (TextUtils.equals(type, "con")) {
                //进入会议详情页
                String url = bundle.getString("url");
                if (TextUtils.isEmpty(url)) {
                    //url 为空 进入会议首页
                    toMain(context);
                } else {
                    Intent intent = new Intent(context, DetailActivity.class);
                    intent.putExtra("id", id);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("type", type);
                    intent.putExtra("url", url);
                    intent.putExtra("flag", 1);
                    context.startActivity(intent);
                }
            }
        } else {
            toMain(context);
        }
    }

    void toMain(Context context) {
        Intent intent = new Intent(context, SplashActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}